package com.frontend.screens;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import com.frontend.*;

/**
 * This class allows the user to change his/her password in the system
 * 
 * @author Elizabeth
 * @author Taylor
 * 
 */
public class ChangePasswordScreen extends Screen {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1940707448695787507L;
	private JLabel message, currentPasswordLabel, newPasswordLabel,
			confirmNewPasswordLabel;
	private JPanel mainPanel, buttonsPanel;
	private JPanel b1, b2, b5, b6;
	private JPasswordField currentPassword, newPassword, confirmNewPassword;
	private JButton submit, back;
	private boolean fadeout, fadein, block;
	private javax.swing.Timer timer;
	private int cycles;
	private String userToChange;
	private Screen previousScreen;
	private Color bgStandard;

	/**
	 * Creates a new screen so that the user can change his/her password
	 * 
	 * @param window
	 * @param username
	 * @param previousScreen
	 */
	public ChangePasswordScreen(DoctorsOfficeClient window, String username, Screen previousScreen) {
		super(window);

		this.previousScreen = previousScreen;
		
		userToChange = username;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		timer = new javax.swing.Timer(50, new AnimationListener());
		ChangePasswordListener cpl = new ChangePasswordListener();
		fadeout = false;
		fadein = false;

		b1 = new JPanel();
		b1.setOpaque(false);

		if (window.getCurrentUser().getUsername().equals(userToChange))
			message = new JLabel("Change your Password");
		else
			message = new JLabel("Change Password for: " + userToChange);
		message.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);

		currentPasswordLabel = new JLabel("Current Password:  ");
		currentPasswordLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		currentPassword = new JPasswordField();
		currentPassword.addActionListener(cpl);

		newPasswordLabel = new JLabel("New Password:  ");
		newPasswordLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		newPassword = new JPasswordField();
		newPassword.addActionListener(cpl);

		confirmNewPasswordLabel = new JLabel("Confirm New Password:  ");
		confirmNewPasswordLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		confirmNewPassword = new JPasswordField();
		confirmNewPassword.addActionListener(cpl);
		
		bgStandard = currentPassword.getBackground();

		boolean differentUser = !window.getCurrentUser().getUsername()
				.equals(userToChange);

		mainPanel = new JPanel();
		mainPanel.setOpaque(false);
		if (differentUser)
			mainPanel.setLayout(new GridLayout(3, 2));
		else
			mainPanel.setLayout(new GridLayout(5, 2));
		if (!differentUser) {
			mainPanel.add(currentPasswordLabel);
			mainPanel.add(currentPassword);
			mainPanel.add(Box.createGlue());
			mainPanel.add(Box.createGlue());
		}
		mainPanel.add(newPasswordLabel);
		mainPanel.add(newPassword);
		mainPanel.add(Box.createGlue());
		mainPanel.add(Box.createGlue());
		mainPanel.add(confirmNewPasswordLabel);
		mainPanel.add(confirmNewPassword);

		b5 = new JPanel();
		b5.setOpaque(false);

		buttonsPanel = new JPanel();
		buttonsPanel.setOpaque(false);
		submit = new JButton("Change Password");
		submit.setOpaque(false);
		submit.setAlignmentX(Component.CENTER_ALIGNMENT);
		submit.addActionListener(cpl);
		back = new JButton("Go Back");
		back.setOpaque(false);
		back.setAlignmentX(Component.CENTER_ALIGNMENT);
		back.addActionListener(cpl);

		buttonsPanel.add(submit);
		buttonsPanel.add(back);

		b6 = new JPanel();
		b6.setOpaque(false);

		add(b1);
		add(message);
		add(b2);
		add(mainPanel);
		add(b5);
		add(buttonsPanel);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			currentPasswordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			currentPassword.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			currentPassword.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			newPasswordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			newPassword.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			newPassword
					.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			confirmNewPasswordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			confirmNewPassword.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			confirmNewPassword.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));

			mainPanel.setMaximumSize(new Dimension(getWidth() * 9 / 10,
					getHeight()));

			submit.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			submit.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			back.setMaximumSize(new Dimension(getWidth() / 5, getHeight() / 15));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	private class ChangePasswordListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(back)) {
				window.changeScreen(previousScreen);
				previousScreen.repaint();
			} else {
				currentPassword.setBackground(bgStandard);
				newPassword.setBackground(bgStandard);
				confirmNewPassword.setBackground(bgStandard);
				
				NetworkInterfacer net = window.getNet();
				String curPwd = new String(currentPassword.getPassword());
				String newPwd = new String(newPassword.getPassword());
				String confNewPwd = new String(confirmNewPassword.getPassword());
				block = false;

				if ((curPwd.equals("") && userToChange.equals(window
						.getCurrentUser().getUsername()))
						|| newPwd.equals("")
						|| confNewPwd.equals("")) {
					timer.stop();
					block = true;
					fadeout = true;
					cycles = 0;
					if(curPwd.equals(""))
						currentPassword.setBackground(new Color(255,200,200));
					if(newPwd.equals(""))
						newPassword.setBackground(new Color(255,200,200));
					if(confNewPwd.equals(""))
						confirmNewPassword.setBackground(new Color(255,200,200));
					message.setText("Fields cannot be blank");
					message.setForeground(Color.red);
					timer.start();
				}
				if (!block) {

					try {
						if (!userToChange.equals(window.getCurrentUser()
								.getUsername()) && newPwd.equals(confNewPwd)) {
							net.changeUserPassword(userToChange, newPwd);
							window.changeScreen(new NavigationScreen(window));
						} else if (net.authenticate(userToChange, curPwd)
								&& newPwd.equals(confNewPwd)) {
							net.changeUserPassword(userToChange, newPwd);
							window.changeScreen(new NavigationScreen(window));
						} else if (newPwd.equals(confNewPwd)) {
							timer.stop();
							block = true;
							fadeout = true;
							cycles = 0;
							currentPassword.setBackground(new Color(255,200,200));
							message.setText("Incorrect Current Password.");
							message.setForeground(Color.red);
							timer.start();
						} else {
							timer.stop();
							block = true;
							fadeout = true;
							cycles = 0;
							newPassword.setBackground(new Color(255,200,200));
							confirmNewPassword.setBackground(new Color(255,200,200));
							message.setText("Passwords do not Match.");
							message.setForeground(Color.red);
							timer.start();
						}
					} catch (ConnectionException e1) {
						System.out.println(e1.getMessage());
					}
				}
			}
		}
	}

	private class AnimationListener implements ActionListener {
		private int alphaVal;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cycles == 0){
				alphaVal = 255;
				fadeout = true;
				fadein = false;
			}
			cycles++;

			if (cycles > 20) {
				if (fadeout) {
					alphaVal = alphaVal - 10;
					if (alphaVal < 0)
						alphaVal = 0;
					message.setForeground(new Color(255, 0, 0, alphaVal));
					if (alphaVal == 0) {
						fadeout = false;
						fadein = true;
						message.setText("Welcome to the Doctor's Office!");
					}
				}
				if (fadein) {
					alphaVal += 10;
					if (alphaVal > 255)
						alphaVal = 255;
					message.setForeground(new Color(0, 0, 0, alphaVal));
					if (alphaVal == 255) {
						fadein = false;
						timer.stop();
					}
				}
			}
		}

	}

}
